<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/14/014
 * Time: 14:09
 */

namespace app\api\controller;


use app\admin\model\Seller;
use app\common\controller\Api;

class Sellerdispatch extends Api
{
    protected $noNeedLogin = ['*'];

    /**
     * 某个商家的所有配送时间
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $seller_id = $this->request->request('seller_id');
        $seller = Seller::get($seller_id);
        if(!$seller){
            $this->error('没有这个商家');
        }
        $model = new \app\api\model\Sellerdispatch();
        $dispatch = $model->where('seller_id', $seller_id)->select();
        $this->success('', $dispatch);
    }

    /**
     * 新增配送时间
     * @throws \think\exception\DbException
     */
    public function add()
    {
        $seller_id = $this->request->request('seller_id');
        $dispatchtime = $this->request->request('dispatchtime');
        if (!$seller_id || !$dispatchtime) {
            $this->error(__('Invalid parameters'));
        }
        $seller = Seller::get($seller_id);
        if(!$seller){
            $this->error('没有这个商家');
        }
        $model = new \app\api\model\Sellerdispatch($_POST);
        $result = $model->allowField(true)->save();
        if($result){
            $this->success('成功');
        }
        $this->error('失败');
    }

    /**
     * 修改配送时间
     * @throws \think\exception\DbException
     */
    public function edit(){
        $id = $this->request->request('id');
        $seller_id = $this->request->request('seller_id');
        $dispatchtime = $this->request->request('dispatchtime');
        if (!$seller_id || !$dispatchtime || !$id) {
            $this->error(__('Invalid parameters'));
        }
        $seller = Seller::get($seller_id);
        if(!$seller){
            $this->error('没有这个商家');
        }
        $dipatch = \app\api\model\Sellerdispatch::get($id);
        if(!$dipatch){
            $this->error('没有这条记录');
        }
        $model = new \app\api\model\Sellerdispatch();
        $result = $model->allowField(true)->save($_POST,['id' => $id]);
        if($result){
            $this->success('成功');
        }
        $this->error('失败');

    }

    /**
     * 删除配送时间
     * 判断是否有权限删除
     * @throws \think\exception\DbException
     */
    public function delete(){
        $id = $this->request->request('id');
        $dipatch = \app\api\model\Sellerdispatch::get($id);
        if(!$dipatch){
            $this->error('没有这条记录');
        }
        $result = $dipatch->delete();
        if($result){
            $this->success('成功');
        }
        $this->error('失败');
    }


}